"""
author:田新宇,俞婧
create time:2020-07-16
update time:2020-07-23
"""
from django.shortcuts import render,redirect
from user.models import course,course_user
from django.forms.models import model_to_dict
from django.db.models import Q
# Create your views here.
def course_page(request):
    page = request.GET.get('current_page', 1)

    course_obj = course.objects.all()[(page-1)*10,page*10]
    lilst_course=[]
    for co in course_obj:
        collections={}
        collections['id'] = co.id
        collections['course_name']= co.course_name
        collections['course_school'] = co.course_school
        collections['course_img'] = co.course_img.url
        lilst_course.append(collections)
    return render(request,'course/course.html', {'course_obj':lilst_course})

def course_detail(request):
    id=request.GET.get('id')
    course_obj = course.objects.get(id=id)
    result = model_to_dict(course_obj)
    result['course_img'] = course_obj.course_img.url
    print(result)
    return render(request,'course/course_detail.html', {'course_obj':result})
#选课接口
def course_choose(request):
    courseStudent = request.session['user_id']
    if request.method == "POST":
        course_add_id = request.POST.get('course_id')
        print(course_add_id)
        new_course_user = course_user.objects.create(course_id=course_add_id, user_id=courseStudent)
        new_course_user.save()
    return redirect('/user_center/')
#搜索框按课程名进行模糊搜索方法
def search_course(request):
    course_name = request.GET.get('course_name')
    course_obj=course.objects.filter(course_name__icontains=course_name)
    lilst_course = []
    for co in course_obj:
        collections = {}
        collections['id'] = co.id
        collections['course_name'] = co.course_name
        collections['course_school'] = co.course_school
        collections['course_img'] = co.course_img.url
        lilst_course.append(collections)
    return render(request, 'course/course.html', {'course_obj': lilst_course})
#搜索框按学校名进行模糊搜索方法
def search_course(request):
    course_name = request.GET.get('course_name')
    course_obj=course.objects.filter(Q(course_school__icontains=course_name) | Q(course_name__icontains=course_name))
    lilst_course = []
    for co in course_obj:
        collections = {}
        collections['id'] = co.id
        collections['course_name'] = co.course_name
        collections['course_school'] = co.course_school
        collections['course_img'] = co.course_img.url
        lilst_course.append(collections)
    return render(request, 'course/course.html', {'course_obj': lilst_course})

#按学校分类
def course_school(request):
    course_school = request.GET.get('course_school')
    course_obj=course.objects.filter(course_school=course_school)
    lilst_course = []
    for co in course_obj:
        collections = {}
        collections['id'] = co.id
        collections['course_name'] = co.course_name
        collections['course_school'] = co.course_school
        collections['course_img'] = co.course_img.url
        lilst_course.append(collections)
    return render(request, 'course/course.html', {'course_obj': lilst_course})
#按课程类型搜索
def course_type(request):
    course_type = request.GET.get('course_type')
    course_obj=course.objects.filter(course_type=course_type)
    lilst_course = []
    for co in course_obj:
        collections = {}
        collections['id'] = co.id
        collections['course_name'] = co.course_name
        collections['course_school'] = co.course_school
        collections['course_img'] = co.course_img.url
        lilst_course.append(collections)
    return render(request, 'course/course.html', {'course_obj': lilst_course})